<?php
$loader->requireOnce('includes/CommonConstant.class.php');
/**
 * GeneralDiagnosis.
 * @package core.ordo
 * @author hdviet
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class GeneralDiagnosis extends ORDataObject {

	/**#@+
	 * Fields of table: general_diagnosis mapped to class members
	 */
	var $general_diagnosis_id	=	'';
	var $glucose		= '';
	var $enum_painful_status	=	'';
	var $ticket_id		= '';
	var $allergy		= '';
	var $general_allergy_detail	=	'';
	var $drug_allergy_detail	=	'';
	var $enum_glasgow_e		= '';
	var $enum_glasgow_m		= '';
	var $enum_glasgow_v		= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'general_diagnosis';

	/**
	 * Primary Key
	 */
	var $_key = 'general_diagnosis_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'GeneralDiagnosis';

	/**
	 * Handle instantiation
	 */
	function GeneralDiagnosis() {
		parent::ORDataObject();
	}
	/**
	 * Get general diagnosis of a given ticketId
	 * @param $ticketId
	 * @return
	 * array(general_diagnosis_id, glucose, enum_paintful_status, allergy, general allergy_detail, drug_allergy_detail, enum_glasgow_e, enum_glasgow_v, enum_glasgow_m)
	 *
	 */
	public function getGeneralDiagnosis($ticketId) {

		$ds = new Datasource_sql();
		$sql = array(
            'cols'  => "general_diagnosis_id, glucose, enum_painful_status, allergy, general_allergy_detail, drug_allergy_detail, enum_glasgow_e, enum_glasgow_v, enum_glasgow_m",
            'from'  => " $this->_table  ",
            'where' => "ticket_id = $ticketId"
		);
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) {
			return null;
		}
		return $result[0];
	}
	/**
	 * save general diagnosis.
	 * @param $ticketId
	 * @param $data
	 * @return unknown_type
	 */
	public function saveGeneralDiagnosis($data = array()) {
	   if (!isset($data['ticket_id'])) {
            throw new InpatientCoreException("lack of ticketId");
        }
		$g = $this->getGeneralDiagnosis($data['ticket_id']);
		if ($g == null) {
			$this->set_id('');
		} else {
			$this->setup($g['general_diagnosis_id']);
		}
		$this->populateArray($data);			
		$this->persist();
		return $this->get_id();	
	}
    /**
     * Convert null number to displayable value (-1) -> ''
     * @param $generalDiagnosis
     * @return nothing.
     */
    public static function normalizeData4Display(&$generalDiagnosis) {
        if (is_array($generalDiagnosis)) {
            if ($generalDiagnosis['glucose'] == CommonConstant::DB_NULL_POSITIVE_NUMBER) {
                $generalDiagnosis['glucose'] = CommonConstant::DB_NULL_DISPLAY_POSITIVE_NUMBER;
            }
            $generalDiagnosis['glucose'] = L10nHelper::renderNumber($generalDiagnosis['glucose'],1);
        }
    }
    
    public function saveAllergyInfo($ticketId, $allergy, $generalAllergy, $drugAllergy){
    	if(!isset($ticketId)){
    		throw new InpatientCoreException("lack of ticketId");
    	}
    	$g = $this->getGeneralDiagnosis($ticketId);
    	if($g == null){
    		$this->set_id('');
    	}
    	else{
    		$this->setup($g['general_diagnosis_id']);
    	}
    	$this->set('ticket_id', $ticketId);
    	$this->set('allergy', $allergy);
    	$this->set('general_allergy_detail', $generalAllergy);
    	$this->set('drug_allergy_detail', $drugAllergy);
    	$this->persist();
    	return $this->get_id();
    }

}
?>
